package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.maiji.cloud.entities.shopingmall.ShopingCart;
import com.maiji.cloud.entities.shopingmall.ShopingOrderMiddleShopingCart;
import com.maiji.cloud.mapper.ShopingOrderMiddleShopingCartMapper;
import com.maiji.cloud.response.shopingmall.ShopingCartResData;
import com.maiji.cloud.service.ShopingCartService;
import com.maiji.cloud.service.ShopingOrderMiddleShopingCartService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.utils.UUID_MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class ShopingOrderMiddleShopingCartServiceImpl extends ServiceImpl<ShopingOrderMiddleShopingCartMapper, ShopingOrderMiddleShopingCart>
        implements ShopingOrderMiddleShopingCartService {

    @Autowired
    private ShopingCartService shopingCartService;

    @Override
    public List<ShopingCartResData> findShopingCartResDataListByOrderId(String orderId) {
        List<ShopingOrderMiddleShopingCart> somscList = selectList(new EntityWrapper<ShopingOrderMiddleShopingCart>().eq("order_id", orderId));
        List<String> scIds = somscList.parallelStream().map(ShopingOrderMiddleShopingCart::getShopingCartId).collect(Collectors.toList());
        List<ShopingCart> shopingCarts = shopingCartService.selectBatchIds(scIds);
        return shopingCartService.findAllResData(shopingCarts);
    }

    @Override
    public Boolean orderSubmitSave (String orderId, List<String> shopingCartIds) {
        List<ShopingOrderMiddleShopingCart> somscs = shopingCartIds.parallelStream().map(scId -> new ShopingOrderMiddleShopingCart()
                .setUuId(UUID_MD5.getUUID()).setShopingCartId(scId).setOrderId(orderId)).collect(Collectors.toList());
        return insertBatch(somscs);
    }

}
